package com.lhd.service;

import com.lhd.dao.TypeDao;
import com.lhd.po.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by limi on 2017/10/16.
 */
@Service
public class TypeServiceImpl implements TypeService {

    @Autowired
    private TypeDao typeDao;

    @Transactional
    @Override
    public Integer saveType(Type type) {
        return typeDao.save(type);
    }

    @Transactional
    @Override
    public Type getType(Long id) {
        return typeDao.findOne(id);
    }

    @Override
    public Type getTypeByName(String name) {
        return typeDao.findByName(name);
    }

    @Transactional
    @Override
    public List<Type> listType(Integer page) {
        return typeDao.findAllByPage(page*10);
    }

    @Override
    public List<Type> listType() {
        return typeDao.findAll();
    }

    @Transactional
    @Override
    public Integer updateType(Long id, Type type) {

        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("name",type.getName());
        return typeDao.update(map);
//        Type t = typeDao.findOne(id);
//        if (t == null) {
//            throw new NotFoundException("不存在该类型");
//        }
//        BeanUtils.copyProperties(type,t);
//        return typeDao.save(t);
    }



    @Transactional
    @Override
    public Long deleteType(Long id) {
        return typeDao.delete(id);
    }

    @Override
    public Integer countType() {
        return typeDao.countType();
    }

    @Override
    public Integer countTypeByTypeId(Long typeId){
        return typeDao.countTypeByTypeId(typeId);
    }

    @Override
    public List<Type> listTypeTop(Integer i) {
        return typeDao.listTypeTop(i);
    }
}
